<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>$nextTick理解 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="什么是$nextTick对于官网的解释是在下次Dom更新循环结束之后执行的延迟回调。在修改数据之后立即使用找个方法，获取更新后的Dom。而简单的回答：由于异步更新队列，$nextTick是用来知道什么时候Dom更新完的。">
<meta property="og:type" content="article">
<meta property="og:title" content="$nextTick理解">
<meta property="og:url" content="http://yoursite.com/2020/07/04/$nextTick%E7%90%86%E8%A7%A3/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="什么是$nextTick对于官网的解释是在下次Dom更新循环结束之后执行的延迟回调。在修改数据之后立即使用找个方法，获取更新后的Dom。而简单的回答：由于异步更新队列，$nextTick是用来知道什么时候Dom更新完的。">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1593868080090&di=7b92262131a4875faa906b8ae269f703&imgtype=0&src=http://pic2.zhimg.com/50/v2-c8bd9e1519cbbbbc6931bfe72cbc794e_hd.jpg">
<meta property="article:published_time" content="2020-07-04T09:00:18.000Z">
<meta property="article:modified_time" content="2020-07-30T13:04:01.592Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="Vue">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1593868080090&di=7b92262131a4875faa906b8ae269f703&imgtype=0&src=http://pic2.zhimg.com/50/v2-c8bd9e1519cbbbbc6931bfe72cbc794e_hd.jpg">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 5.0.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-$nextTick理解" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/07/04/$nextTick%E7%90%86%E8%A7%A3/" class="article-date">
  <time datetime="2020-07-04T09:00:18.000Z" itemprop="datePublished">2020-07-04</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      $nextTick理解
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h4 id="什么是-nextTick"><a href="#什么是-nextTick" class="headerlink" title="什么是$nextTick"></a>什么是$nextTick</h4><p>对于官网的解释是在下次Dom更新循环结束之后执行的延迟回调。在修改数据之后立即使用找个方法，获取更新后的Dom。而简单的回答：由于异步更新队列，$nextTick是用来知道什么时候Dom更新完的。<br><img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1593868080090&di=7b92262131a4875faa906b8ae269f703&imgtype=0&src=http://pic2.zhimg.com/50/v2-c8bd9e1519cbbbbc6931bfe72cbc794e_hd.jpg" alt="$nextTick"></p>
<a id="more"></a>
<p>下面我们来一段代码:</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;app&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;show&quot;</span> <span class="attr">v-if</span>=<span class="string">&quot;showDiv&quot;</span>&gt;</span>显示文章<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;showAndGetText&quot;</span>&gt;</span>获取内容<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> app = <span class="keyword">new</span> Vue(&#123;</span><br><span class="line">          el: <span class="string">&#x27;#app&#x27;</span>,</span><br><span class="line">          data() &#123;</span><br><span class="line">              <span class="keyword">return</span> &#123;</span><br><span class="line">                  showDiv: <span class="literal">false</span></span><br><span class="line">              &#125;</span><br><span class="line">          &#125;,</span><br><span class="line">          methods: &#123;</span><br><span class="line">              showAndGetText() &#123;</span><br><span class="line">                  <span class="built_in">this</span>.showDiv = <span class="literal">true</span>;</span><br><span class="line">                  <span class="keyword">let</span> text = <span class="built_in">document</span>.getElementsByClassName(<span class="string">&quot;show&quot;</span>).innerHTML;</span><br><span class="line">                  <span class="built_in">console</span>.log(text)</span><br><span class="line">              &#125;</span><br><span class="line">          &#125;</span><br><span class="line">      &#125;)</span><br></pre></td></tr></table></figure>
<p>这段代码不是很难理解，在控制台会看到一个 <code>innerHTML of null</code>的错误，因为此时页面并没有渲染，它也没有获取到div元素，这里涉及到Vue的一个重要概念：异步更新队列。</p>
<h6 id="Vue在观察到数据变化并不是渲染Dom，而是开启一个队列，并缓冲在同一事件循环中发生的所有数据变化。"><a href="#Vue在观察到数据变化并不是渲染Dom，而是开启一个队列，并缓冲在同一事件循环中发生的所有数据变化。" class="headerlink" title="Vue在观察到数据变化并不是渲染Dom，而是开启一个队列，并缓冲在同一事件循环中发生的所有数据变化。"></a>Vue在观察到数据变化并不是渲染Dom，而是开启一个队列，并缓冲在同一事件循环中发生的所有数据变化。</h6><p>  在缓冲时会去除重复的数据，这样避免了不必要的计算和Dom操作。然后，在下一个事件循环Tick中，Vue刷新队列并且执行已去重的数据。<br>  所以，用一个for循环在动态改变数据100次，其实它只是会应用最后一次，如果没有这种机制,Dom就会重绘100次，十分消耗性能<br>  知道了Vue的异步原理之后，就不难理解上诉代码 实际上<code>this.showDiv = true;</code>时，div仍然没有创建出来，下面的<br>  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> text = <span class="built_in">document</span>.getElementsByClassName(<span class="string">&quot;show&quot;</span>).innerHTML;</span><br></pre></td></tr></table></figure><br>  仍然读取的是第一次事件的Dom，而实际上这一次事件循环中，Dom并没有更新，所以是读取不到了。<br>  我们需要等待下一个事件循环，Dom更新完成后，就可读到了。<br>  那时就是$nextTick登场的时候了：<br>  <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> app = <span class="keyword">new</span> Vue(&#123;</span><br><span class="line">           el: <span class="string">&#x27;#app&#x27;</span>,</span><br><span class="line">           data() &#123;</span><br><span class="line">               <span class="keyword">return</span> &#123;</span><br><span class="line">                   showDiv: <span class="literal">false</span></span><br><span class="line">               &#125;</span><br><span class="line">           &#125;,</span><br><span class="line">           methods: &#123;</span><br><span class="line">               showAndGetText() &#123;</span><br><span class="line">                   <span class="built_in">this</span>.showDiv = <span class="literal">true</span>;</span><br><span class="line">                   <span class="built_in">this</span>.$nextTick(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">                          <span class="keyword">let</span> text = <span class="built_in">document</span>.getElementsByClassName(<span class="string">&quot;show&quot;</span>).innerHTML;</span><br><span class="line">                   &#125;)</span><br><span class="line">                   <span class="built_in">console</span>.log(text)</span><br><span class="line">               &#125;</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;)</span><br></pre></td></tr></table></figure><br>当点击后this.$nextTick是在下次Dom更新之后，执行的回调，从而获取Dom。我们都知道Vue是数据驱动视图的，但是有一些情况，比如；我们想使用一些第三方插件,希望在Dom元素属性变化之后，重新加载插件，这时候就需要在$nextTick的回调中重新执行插件的方法</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/07/04/$nextTick%E7%90%86%E8%A7%A3/" data-id="ckd8udtb700019gt4fmg90j5h" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Vue/" rel="tag">Vue</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2020/07/07/%E6%89%8B%E5%8A%A8%E5%AE%9E%E7%8E%B0call%EF%BC%8Capply,bind/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          手动实现call，apply,bind
        
      </div>
    </a>
  
  
    <a href="/2020/06/28/%E7%BB%84%E4%BB%B6_%E5%A4%84%E7%90%86%E8%BE%B9%E7%95%8C%E6%83%85%E5%86%B5/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">组件_处理边界情况</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Vue/" rel="tag">Vue</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Vue%E5%85%A8%E5%AE%B6%E6%A1%B6/" rel="tag">Vue全家桶</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/js/" rel="tag">js</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/node/" rel="tag">node</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/Vue/" style="font-size: 20px;">Vue</a> <a href="/tags/Vue%E5%85%A8%E5%AE%B6%E6%A1%B6/" style="font-size: 10px;">Vue全家桶</a> <a href="/tags/js/" style="font-size: 15px;">js</a> <a href="/tags/node/" style="font-size: 10px;">node</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/07/">July 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/06/">June 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2020/07/30/hello-world/">我的第一章博客</a>
          </li>
        
          <li>
            <a href="/2020/07/25/%E5%8E%9F%E7%94%9FHttp%E5%AE%9E%E7%8E%B0cookie%E8%AE%BE%E7%BD%AE/">原生Http实现cookie设置</a>
          </li>
        
          <li>
            <a href="/2020/07/23/%E5%B0%81%E8%A3%85Vue%E7%BB%84%E4%BB%B6%E5%B0%86%E4%BB%BB%E6%84%8F%E5%90%8E%E7%BC%80%E6%96%87%E4%BB%B6%E7%9B%AE%E5%BD%95%E7%BB%9F%E4%B8%80%E5%85%A8%E9%83%A8%E5%BC%95%E5%85%A5%E5%88%B0%E5%BD%93%E5%89%8D%E6%96%87%E4%BB%B6/">封装Vue组件将任意后缀文件目录统一全部引入到当前文件</a>
          </li>
        
          <li>
            <a href="/2020/07/15/js%E4%B8%AD%E7%9A%84%E9%98%B2%E6%8A%96%E4%B8%8E%E8%8A%82%E6%B5%81/">js中的防抖与节流</a>
          </li>
        
          <li>
            <a href="/2020/07/07/%E6%89%8B%E5%8A%A8%E5%AE%9E%E7%8E%B0call%EF%BC%8Capply,bind/">手动实现call，apply,bind</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 John Doe<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>




  </div>
</body>
</html>